﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;
using KPIS.GERP.GIMS.DAL;
using KPIS.GERP.GIMS.MODEL;

namespace KPIS.GERP.GIMS.APP.APP_UI.Edit_Form
{
    public partial class ICM_Asset_Register : System.Web.UI.Page
    {
        Common.Main cn = new Common.Main();
        D_Asset d_asset = new D_Asset();
        M_Asset m_asset = new M_Asset();
        D_AssetWarranty d_warranty = new D_AssetWarranty();
        M_AssetWarranty m_warranty = new M_AssetWarranty();
        D_AssetLocation d_location = new D_AssetLocation();
        M_AssetLocation m_location = new M_AssetLocation();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {                
                pAssetCode.Text = Request.QueryString["asset_code"];
                M_Asset info =  d_asset.GetListFillBySeq(pAssetCode.Text);
                pAssetName.Text = info.asset_name.ToString();
                pReceiveCode.Text = info.icm_receive_code.ToString();
                pReceiveDate.Text = string.Format("{0:D}", info.receive_date);
                pPrice.Text = string.Format("{0:N2}", info.price);
                pSerialNumber.Text = info.serial_number.ToString();
                pRemark.Text = info.asset_remark.ToString();
                LoadGridLocation();
                grdLocation.DataBind();
                LoadGridWarranty();
                grdWarranty.DataBind();
            }
            
            //btnAddLocation.OnClientClick = "AddLocation('',"+ pAssetCode.Text + ")";
             
        }
        protected void grdLocation_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            LoadGridLocation();
        }
        protected void grdLocation_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "DataDelete")
            {
                try
                {
                    m_location.location_seq = Convert.ToInt32(grdLocation.Items[e.Item.ItemIndex]["location_seq"].Text);
                    m_location.record_status = SystemInfo._DeleteRecordStatus;
                    m_location.location_end_date = DateTime.Now;
                    m_location.updated_by = cn.UserID;
                    m_location.updated_when = DateTime.Now;
                    d_location.Delete_Locaion(m_location);
                }
                catch (Exception ex)
                {
                    RadAjaxManager1.Alert("เกิดข้อผิดพลาดขณะลบข้อมูล");
                    throw (ex);
                }
                finally
                {
                    LoadGridLocation();
                    grdLocation.Rebind();
                }
            }
        }
        protected void grdWarranty_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            LoadGridWarranty();
        }
        protected void grdWarranty_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "DataDelete")
            {
                try
                {
                    m_warranty.warranty_seq = Convert.ToInt32(grdWarranty.Items[e.Item.ItemIndex]["warranty_seq"].Text);
                    m_warranty.record_status = SystemInfo._DeleteRecordStatus;
                    m_warranty.warranty_end_date = DateTime.Now;
                    d_warranty.Delete_Warranty(m_warranty);
                }
                catch (Exception ex)
                {
                    RadAjaxManager1.Alert("เกิดข้อผิดพลาดขณะลบข้อมูล");
                    throw (ex);
                }
                finally
                {
                    LoadGridWarranty();
                    grdWarranty.Rebind();
                }
            }
        }
        private void LoadGridLocation()
        {
            grdLocation.DataSource = d_location.GetListAssetCode(pAssetCode.Text);
        }
        private void LoadGridWarranty()
        {
            grdWarranty.DataSource = d_warranty.GetListAssetCode(pAssetCode.Text) ;
        }
        protected void btnAddWarranty_OnClick(object sender, EventArgs e)
        {
            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "onclick", "AddWarranty('', '" + pAssetCode.Text + "')", true);
        }
        protected void btnAddLocation_OnClick(object sender, EventArgs e)
        {
            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "onclick", "AddLocation('', '" + pAssetCode.Text + "')", true);
        }
        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            LoadGridLocation();
            grdLocation.Rebind();
            LoadGridWarranty();
            grdWarranty.Rebind();
        }

    }
}
